class Pagination
  def initialize(obj, page_size = 10, sort = nil, sort_order = 'a')
    @data = Array.new(obj)
    @page_size = page_size
    @current_page_index = 0
    @sort = sort
    @sort_order = sort_order
    @current_page_count = 1
    @current_page = []
  end
  
  def get_next_page_index
    @current_page_index + @page_size
  end
  
  def get_previous_page_index
    @current_page_index - @page_size
  end
  
  def get_next_page
    @current_page_index = get_next_page_index
    @current_page_count += 1 
    @current_page = paginate(@current_page_index, @page_size)
  end
  
  def get_previous_page
    @current_page_index = get_previous_page_index
    @current_page_count -= 1
    @current_page = paginate(@current_page_index, @page_size)
  end
  
  def get_total_page_count
    ((@data.size % @page_size) == 0) ? (@data.size / @page_size) : ((@data.size / @page_size) + 1)
  end

  def get_page(index)
    if index < get_total_page_count + 1
      @current_page_index = @page_size * (index - 1)
      @current_page_count = index
      @current_page = paginate(@current_page_index, @page_size)
    end
  end
  
  def size
    @data.size
  end
  
  #~ def generate_page_links
    #~ links = []
    #~ if(@current_page_count == 1 or @current_page_count == 2)
      #~ for i in 1...5
        #~ links << link_to(i, get_page(i))
      #~ end
    #~ else
      #~ for i in -2...3
        #~ links << link_to((@current_page_count + i), get_page(@current_page_count + i)) if(@current_page_count + i) < get_total_page_count
      #~ end
    #~ end
    #~ links << link_to(get_total_page_count, get_page(get_total_page_count))
    #~ links.join(' ')
  #~ end
  
    def generate_page_links
    links = []
    links << "| <a href='getpage?page=1'>First</a> |" if @current_page_count != 1
    #~ if(@current_page_count == 1 or @current_page_count == 2)
      #~ for i in 1...6
        
        #~ links << "<a href='getpage?page=#{i}'>#{i}</a>" 
      #~ end
    #~ else
      for i in -2...3
        if i != 0
          links << "<a href='getpage?page=#{@current_page_count + i}'>#{@current_page_count + i}</a>" if(((@current_page_count + i) < get_total_page_count) and (@current_page_count + i>0))
        else
          links << "#{@current_page_count + i}"
        end
      end
    #~ end
    links << "...   | <a href='getpage?page=#{get_total_page_count}'>Last</a>"  if @current_page_count != get_total_page_count
    links.join(' ')
  end
  
  #~ def generate_next_previous_page_links
    #~ links = []
    #~ puts "<%=link_to('#{get_next_page_index}', next_page)%>"
    #~ links << "<%=link_to('#{get_next_page_index.to_s}', next_page)%>"
    #~ links << ' | '
    #~ links << "<%=link_to('#{get_next_page_index}', next_page)%>"
    #~ puts links.inspect
    #~ links.join(' ')
  #~ end
  
  def generate_next_previous_page_links
    links = []
    links << "<a href='previous_page'>Previous</a>" if get_previous_page_index > -1
    links << ' | '
    links << "<a href='next_page'>Next</a>" if @current_page_count < get_total_page_count
    links.join
  end
  
  def paginate(start_index = 0, no_of_results_per_page = @page_size, sort = false, sort_order = 'a')
    page_entries = []
    @data = sort_objects(@data,sort) if sort != false
    @data.reverse! if sort!= false and sort_order.to_s == ('d')
    no_of_results_per_page.times do |cnt|
      page_entries << @data[start_index + cnt]
    end
    @current_page = page_entries
  end
  
  attr_reader  :data, :page_size, :current_page_index, :sort, :sort_order, :current_page_count, :current_page
end

#~ a =[]
#~ 100.times do |i|
  #~ a << i
#~ end
#~ paginate = Pagination.new(a)

#~ puts '-------- 1 --------'
#~ puts paginate.paginate
#~ puts '-------- 2 --------'
#~ puts paginate.get_next_page
#~ puts '-------- 3 --------'
#~ puts paginate.get_next_page
#~ puts '-------- 4 --------'
#~ puts paginate.get_next_page
#~ puts '-------- 5 --------'
#~ puts paginate.get_next_page
#~ puts '-------- 4 -------- Previous Page'
#~ puts paginate.get_previous_page

#~ puts '-------- 5 -------- Get Page 5'
#~ puts paginate.get_page(11)

#~ puts paginate.get_total_page_count
#~ paginate.get_page(10)
#~ puts paginate.current_page_index
#~ puts paginate.generate_page_links
#~ puts paginate.generate_next_previous_page_links
#~ class PPages
#~ def self.create_pagination(a)
  #~ p 'NAYAK here'
  #~ @paginate = Pagination.new(a)
#~ end
#~ def self.my_mtd
  #~ p 'mymtd'
  #~ p @paginate
#~ end
  #~ attr_accessor :paginate
#~ end